package com.eastcom.providor.dao.impl;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.eastcom.bm.atom.Atom_PPMDefine;
import com.eastcom.canicula.stope.utils.DateUtil;
import com.eastcom.client.PPMdefineInfo;
import com.eastcom.common.util.CharliDate;
import com.eastcom.providor.dao.BaseDao;
import com.eastcom.providor.dao.IPPMDefineDao;

public class PPMDefineDao extends BaseDao implements IPPMDefineDao {

	public List<PPMdefineInfo> selectPPMdefineInfoByParams(String business,String reportName, Date startDate, Date endDate)throws Exception {
		try{
			StringBuffer hql=new StringBuffer("from Atom_PPMDefine");
			boolean firstCondition=appeandNotNullParam(hql, "neType", business, business, "=", true);
			appeandNotNullParam(hql, "neType", reportName, "%"+reportName+"%", "like", true);
			List<Atom_PPMDefine> ppmDefines=this.getObjects(hql.toString());
			if(ppmDefines==null||ppmDefines.isEmpty())
				return null;
			
			List<PPMdefineInfo> results=new LinkedList<PPMdefineInfo>();
			
			String startTime=startDate==null&&endDate==null?DateUtil.DateToString("yyyy-MM-dd HH:mm:ss", CharliDate.getStartTimeOfDaysApartCurrentDay(new Date(), -1)):
				startDate==null?null:DateUtil.DateToString("yyyy-MM-dd HH:mm:ss", startDate);
			
			String endTime=endDate==null?null:DateUtil.DateToString("yyyy-MM-dd HH:mm:ss", endDate);

			StringBuffer sql=new StringBuffer("select p.GROUPID,p.NETYPE,p.REPORTNAME,p.GRANULARITY,p.REPORTPERIOD,p.REPORTDELAY,"+
		         "p.REPORTFILENUM,s.* from qa_ppm_define p,("+
		         "select t2.GROUPID,max(t2.uptime) as uploadtime,count(*) as num,max(t2.datatime) max_datatime,min(t2.datatime) min_datatime"+
		         " from qa_ppm_monitor t2 where (t2.groupid,TRUNC(t2.UPTIME,'hh')) in"+
		         "(select t3.groupid,max(TRUNC(t3.UPTIME,'hh')) from qa_ppm_monitor t3");
			
			firstCondition=this.appendNotNullDateAfterCondition(sql, "t3.uptime", startTime, true);
			appendNotNullDateBeforeCondition(sql, "t3.uptime", endTime, firstCondition);
			sql.append("group by t3.groupid) group by t2.GROUPID) s ");
			sql.append("where p.GROUPID=s.GROUPID");
			appeandNotNullParam(sql, "p.netype", business, business, "=", false);
			appeandNotNullParam(sql, "p.reportname", reportName, "%"+reportName+"%", "like", false);
			appeandNotNullParam(sql, "p.GRANULARITY", "3600", "3600", "=", false);
			Map<Object,Map> ppmStateMap=this.getFieldMapWithKeyFildBySQL(sql.toString(), "groupid", new String[]{"netype","reportname",
				"granularity","reportperiod","reportdelay","reportfilenum","uploadtime","num","max_datatime","min_datatime"});
			
			for(Atom_PPMDefine ppmDefine:ppmDefines){
				if(!"3600".equals(ppmDefine.getGranularity()))
					continue;
				
				Map stateMap=ppmStateMap==null?null:ppmStateMap.get(ppmDefine.getGroupId());
				PPMdefineInfo ppmdefineInfo=new PPMdefineInfo();
				ppmdefineInfo.setGroupId(ppmDefine.getGroupId());
				ppmdefineInfo.setBusiness(stateMap==null?null:(String)stateMap.get("netype"));
				ppmdefineInfo.setReportName(stateMap==null?null:(String)stateMap.get("reportname"));
				ppmdefineInfo.setGranularity(stateMap==null||stateMap.get("granularity")==null?null:(String)stateMap.get("granularity").toString());
				ppmdefineInfo.setReportPeriod(stateMap==null||stateMap.get("reportperiod")==null?null:stateMap.get("reportperiod").toString());
				ppmdefineInfo.setReportDelay(stateMap==null||stateMap.get("reportdelay")==null?null:stateMap.get("reportdelay").toString());
				ppmdefineInfo.setReportFilenum(stateMap==null||stateMap.get("reportfilenum")==null?null:stateMap.get("reportfilenum").toString());
				ppmdefineInfo.setFileNum(stateMap==null||stateMap.get("num")==null?null:stateMap.get("num").toString());
				ppmdefineInfo.setUploadTime(stateMap==null?null:(Date)stateMap.get("uploadtime"));
				ppmdefineInfo.setMin_datatime(stateMap==null?null:(Date)stateMap.get("min_datatime"));
				ppmdefineInfo.setMax_datatime(stateMap==null?null:(Date)stateMap.get("max_datatime"));
				
				results.add(ppmdefineInfo);
			}
			
			sql=new StringBuffer("select p.GROUPID,p.NETYPE,p.REPORTNAME,p.GRANULARITY,p.REPORTPERIOD,p.REPORTDELAY,"+
			    "p.REPORTFILENUM,s.* from qa_ppm_define p,("+
			    "select t2.GROUPID,max(t2.uptime) as uploadtime,count(*) as num,max(t2.datatime) max_datatime,min(t2.datatime) min_datatime"+
			    " from qa_ppm_monitor t2 where (t2.groupid,TRUNC(t2.UPTIME,'dd')) in"+
			    "(select t3.groupid,max(TRUNC(t3.UPTIME,'dd')) from qa_ppm_monitor t3");
			
		    firstCondition=this.appendNotNullDateAfterCondition(sql, "t3.uptime", startTime, true);
			appendNotNullDateBeforeCondition(sql, "t3.uptime", endTime, firstCondition);
			sql.append("group by t3.groupid) group by t2.GROUPID) s ");
			sql.append("where p.GROUPID=s.GROUPID");
			appeandNotNullParam(sql, "p.netype", business, business, "=", false);
			appeandNotNullParam(sql, "p.reportname", reportName, "%"+reportName+"%", "like", false);
			appeandNotNullParam(sql, "p.GRANULARITY", "86400", "86400", "=", false);
			ppmStateMap=this.getFieldMapWithKeyFildBySQL(sql.toString(), "groupid", new String[]{"netype","reportname",
				"granularity","reportperiod","reportdelay","reportfilenum","uploadtime","num","max_datatime","min_datatime"});
			
			for(Atom_PPMDefine ppmDefine:ppmDefines){
				if("3600".equals(ppmDefine.getGranularity()))
					continue;
				
				Map stateMap=ppmStateMap==null?null:ppmStateMap.get(ppmDefine.getGroupId());
				PPMdefineInfo ppmdefineInfo=new PPMdefineInfo();
				ppmdefineInfo.setGroupId(ppmDefine.getGroupId());
				ppmdefineInfo.setBusiness(stateMap==null?null:(String)stateMap.get("netype"));
				ppmdefineInfo.setReportName(stateMap==null?null:(String)stateMap.get("reportname"));
				ppmdefineInfo.setGranularity(stateMap==null||stateMap.get("granularity")==null?null:(String)stateMap.get("granularity").toString());
				ppmdefineInfo.setReportPeriod(stateMap==null||stateMap.get("reportperiod")==null?null:stateMap.get("reportperiod").toString());
				ppmdefineInfo.setReportDelay(stateMap==null||stateMap.get("reportdelay")==null?null:stateMap.get("reportdelay").toString());
				ppmdefineInfo.setReportFilenum(stateMap==null||stateMap.get("reportfilenum")==null?null:stateMap.get("reportfilenum").toString());
				ppmdefineInfo.setFileNum(stateMap==null||stateMap.get("num")==null?null:stateMap.get("num").toString());
				ppmdefineInfo.setUploadTime(stateMap==null?null:(Date)stateMap.get("uploadtime"));
				ppmdefineInfo.setMin_datatime(stateMap==null?null:(Date)stateMap.get("min_datatime"));
				ppmdefineInfo.setMax_datatime(stateMap==null?null:(Date)stateMap.get("max_datatime"));
				
				results.add(ppmdefineInfo);
			}
			return results;
		}
		catch(Exception e){
			throw e;
		}

	}

	public List<Atom_PPMDefine> selectPPMDefineByParams(String business, String reportName) throws Exception {
		try{
			StringBuffer hql=new StringBuffer("from Atom_PPMDefine");
			boolean firstCondition=appeandNotNullParam(hql, "neType", business, business, "=", true);
			appeandNotNullParam(hql, "reportName", reportName, "%"+reportName+"%", "like", firstCondition);
			return this.getObjects(hql.toString());
		}
		catch(Exception e){
			throw e;
		}
	}

	public List<PPMdefineInfo> selectPPMdefineHistoryInfoByParams(String business, String reportName, Date startDate, Date endDate) throws Exception {
        try{
        	StringBuffer hql=new StringBuffer("from Atom_PPMDefine");
			boolean firstCondition=appeandNotNullParam(hql, "neType", business, business, "=", true);
			appeandNotNullParam(hql, "reportName", reportName, "%"+reportName+"%", "like", firstCondition);
			List<Atom_PPMDefine> ppmDefines=this.getObjects(hql.toString());
			if(ppmDefines==null||ppmDefines.isEmpty())
				return null;
			
			List<PPMdefineInfo> results=new LinkedList<PPMdefineInfo>();
			
			String startTime=startDate==null&&endDate==null?DateUtil.DateToString("yyyy-MM-dd HH:mm:ss", CharliDate.getStartTimeOfDaysApartCurrentDay(new Date(), -1)):
				startDate==null?null:DateUtil.DateToString("yyyy-MM-dd HH:mm:ss", startDate);
			
			String endTime=endDate==null?null:DateUtil.DateToString("yyyy-MM-dd HH:mm:ss", endDate);

			StringBuffer sql=new StringBuffer("select p.GROUPID,p.NETYPE,p.REPORTNAME,p.GRANULARITY,p.REPORTPERIOD,p.REPORTDELAY,"+
		         "p.REPORTFILENUM,s.* from qa_ppm_define p,("+
		         "select t2.GROUPID,max(t2.uptime) as uploadtime,count(*) as num,max(t2.datatime) max_datatime,min(t2.datatime) min_datatime"+
		         " from qa_ppm_monitor t2 where (t2.groupid,TRUNC(t2.UPTIME,'hh')) in"+
		         "(select t3.groupid,TRUNC(t3.UPTIME,'hh') from qa_ppm_monitor t3");
			
			firstCondition=this.appendNotNullDateAfterCondition(sql, "t3.uptime", startTime, true);
			appendNotNullDateBeforeCondition(sql, "t3.uptime", endTime, firstCondition);
			sql.append("group by t3.groupid,TRUNC(t3.UPTIME,'hh')) group by t2.GROUPID,TRUNC(t2.UPTIME,'hh')) s ");
			sql.append("where p.GROUPID=s.GROUPID");
			appeandNotNullParam(sql, "p.netype", business, business, "=", false);
			appeandNotNullParam(sql, "p.reportname", reportName, "%"+reportName+"%", "like", false);
			appeandNotNullParam(sql, "p.GRANULARITY", "3600", "3600", "=", false);
			sql.append(" order by p.GROUPID,s.uploadtime");
			Map<Object,List<Map>> ppmStateMap=this.getFieldMapLstWithKeyFildBySQL(sql.toString(), "groupid", new String[]{"netype","reportname",
				"granularity","reportperiod","reportdelay","reportfilenum","uploadtime","num","max_datatime","min_datatime"});
			
			for(Atom_PPMDefine ppmDefine:ppmDefines){
				if(!"3600".equals(ppmDefine.getGranularity()))
					continue;
				
				List<Map> statelist=ppmStateMap==null?null:ppmStateMap.get(ppmDefine.getGroupId());
				if(statelist==null||statelist.isEmpty()){
					PPMdefineInfo ppmdefineInfo=new PPMdefineInfo();
					ppmdefineInfo.setBusiness(ppmDefine.getNeType());
					ppmdefineInfo.setGranularity(ppmDefine.getGranularity());
					ppmdefineInfo.setReportName(ppmDefine.getReportName());
					ppmdefineInfo.setGroupId(ppmDefine.getGroupId());
					ppmdefineInfo.setReportPeriod(ppmDefine.getReportPeriod());
					ppmdefineInfo.setReportFilenum(ppmDefine.getReportFilenum());
					ppmdefineInfo.setReportDelay(ppmDefine.getReportDelay());
					ppmdefineInfo.setFileNum("0");
					
					results.add(ppmdefineInfo);
				}
				else{
				    for(Map stateMap:statelist){
				        PPMdefineInfo ppmdefineInfo=new PPMdefineInfo();
				        ppmdefineInfo.setBusiness(stateMap==null?null:(String)stateMap.get("netype"));
				        ppmdefineInfo.setReportName(stateMap==null?null:(String)stateMap.get("reportname"));
				        ppmdefineInfo.setGranularity(stateMap==null||stateMap.get("granularity")==null?null:(String)stateMap.get("granularity").toString());
				        ppmdefineInfo.setReportPeriod(stateMap==null||stateMap.get("reportperiod")==null?null:stateMap.get("reportperiod").toString());
				        ppmdefineInfo.setReportDelay(stateMap==null||stateMap.get("reportdelay")==null?null:stateMap.get("reportdelay").toString());
				        ppmdefineInfo.setReportFilenum(stateMap==null||stateMap.get("reportfilenum")==null?null:stateMap.get("reportfilenum").toString());
				        ppmdefineInfo.setFileNum(stateMap==null||stateMap.get("num")==null?null:stateMap.get("num").toString());
				        ppmdefineInfo.setUploadTime(stateMap==null?null:(Date)stateMap.get("uploadtime"));
				        ppmdefineInfo.setMin_datatime(stateMap==null?null:(Date)stateMap.get("min_datatime"));
				        ppmdefineInfo.setMax_datatime(stateMap==null?null:(Date)stateMap.get("max_datatime"));

				        results.add(ppmdefineInfo);
				    }
				}
			}
			
			sql=new StringBuffer("select p.GROUPID,p.NETYPE,p.REPORTNAME,p.GRANULARITY,p.REPORTPERIOD,p.REPORTDELAY,"+
			         "p.REPORTFILENUM,s.* from qa_ppm_define p,("+
			         "select t2.GROUPID,max(t2.uptime) as uploadtime,count(*) as num,max(t2.datatime) max_datatime,min(t2.datatime) min_datatime"+
			         " from qa_ppm_monitor t2 where (t2.groupid,TRUNC(t2.UPTIME,'dd')) in"+
			         "(select t3.groupid,TRUNC(t3.UPTIME,'dd') from qa_ppm_monitor t3");
			
			firstCondition=this.appendNotNullDateAfterCondition(sql, "t3.uptime", startTime, true);
			appendNotNullDateBeforeCondition(sql, "t3.uptime", endTime, firstCondition);
			sql.append("group by t3.groupid,TRUNC(t3.UPTIME,'dd')) group by t2.GROUPID,TRUNC(t2.UPTIME,'dd')) s ");
			sql.append("where p.GROUPID=s.GROUPID");
			appeandNotNullParam(sql, "p.netype", business, business, "=", false);
			appeandNotNullParam(sql, "p.reportname", reportName, "%"+reportName+"%", "like", false);
			appeandNotNullParam(sql, "p.GRANULARITY", "86400", "86400", "=", false);
			sql.append(" order by p.GROUPID,s.uploadtime");
			ppmStateMap=this.getFieldMapLstWithKeyFildBySQL(sql.toString(), "groupid", new String[]{"netype","reportname",
				"granularity","reportperiod","reportdelay","reportfilenum","uploadtime","num","max_datatime","min_datatime"});
				
			for(Atom_PPMDefine ppmDefine:ppmDefines){
				if("3600".equals(ppmDefine.getGranularity()))
					continue;
				
				List<Map> statelist=ppmStateMap==null?null:ppmStateMap.get(ppmDefine.getGroupId());
				if(statelist==null||statelist.isEmpty()){
					PPMdefineInfo ppmdefineInfo=new PPMdefineInfo();
					ppmdefineInfo.setBusiness(ppmDefine.getNeType());
					ppmdefineInfo.setGranularity(ppmDefine.getGranularity());
					ppmdefineInfo.setReportName(ppmDefine.getReportName());
					ppmdefineInfo.setGroupId(ppmDefine.getGroupId());
					ppmdefineInfo.setReportPeriod(ppmDefine.getReportPeriod());
					ppmdefineInfo.setReportFilenum(ppmDefine.getReportFilenum());
					
					results.add(ppmdefineInfo);
				}
				else{
				    for(Map stateMap:statelist){
				        PPMdefineInfo ppmdefineInfo=new PPMdefineInfo();
				        ppmdefineInfo.setBusiness(stateMap==null?null:(String)stateMap.get("netype"));
				        ppmdefineInfo.setReportName(stateMap==null?null:(String)stateMap.get("reportname"));
				        ppmdefineInfo.setGranularity(stateMap==null||stateMap.get("granularity")==null?null:(String)stateMap.get("granularity").toString());
				        ppmdefineInfo.setReportPeriod(stateMap==null||stateMap.get("reportperiod")==null?null:stateMap.get("reportperiod").toString());
				        ppmdefineInfo.setReportDelay(stateMap==null||stateMap.get("reportdelay")==null?null:stateMap.get("reportdelay").toString());
				        ppmdefineInfo.setReportFilenum(stateMap==null||stateMap.get("reportfilenum")==null?null:stateMap.get("reportfilenum").toString());
				        ppmdefineInfo.setFileNum(stateMap==null||stateMap.get("num")==null?null:stateMap.get("num").toString());
				        ppmdefineInfo.setUploadTime(stateMap==null?null:(Date)stateMap.get("uploadtime"));
				        ppmdefineInfo.setMin_datatime(stateMap==null?null:(Date)stateMap.get("min_datatime"));
				        ppmdefineInfo.setMax_datatime(stateMap==null?null:(Date)stateMap.get("max_datatime"));
				
				        results.add(ppmdefineInfo);
			        }
				}
			}
			return results;
		}
		catch(Exception e){
			throw e;
		}
	}

}
